<template>
    <div>
      <h1>处理订单</h1>
      <md-field>

        <md-field-item
          name="name"
          title="车辆类型"
          align="left"
          :value="orderinfo.car.name">
        </md-field-item>
        <md-field-item
          name="name"
          title="联系人"
          align="left"
          :value="orderinfo.lxname">
        </md-field-item>
        <md-field-item
          name="name"
          title="电话"
          align="left"
          :value="orderinfo.phone">
        </md-field-item>
        <md-field-item
          name="name"
          title="取车时间"
          align="left"
          :value="new Date(form.sdate).toLocaleString()">
        </md-field-item>
        <md-field-item
          v-if="form.edate"
          name="name"
          title="还车时间"
          align="left"
          :value="new Date(form.edate).toLocaleString()">
        </md-field-item>
        <md-field-item
          name="name"
          title="订单状态"
          align="left"
        >
          <span v-if="orderinfo.state===-1">已取消</span>
          <span v-if="orderinfo.state===0">未支付</span>
          <span v-if="orderinfo.state===1">等待取车</span>
          <span v-if="orderinfo.state===2">等待还车</span>
          <span v-if="orderinfo.state===3">违章确认期</span>
          <span v-if="orderinfo.state===4">有违章需要处理</span>
          <span v-if="orderinfo.state===5">已完成</span>
        </md-field-item>
        <md-field-item
          name="name"
          title="用车时间"
          align="left"
        >
          <span >{{daycount}}天</span>
        </md-field-item>
        <md-field-item
          name="name"
          title="已支付定金"
          align="left"
        >
          <span >{{orderinfo.order_pay}}元</span>
        </md-field-item>
        <md-field-item
          name="name"
          title="取车地址"
          align="left"
          :value="orderinfo.address">
        </md-field-item>

        <md-input-item
          title="车牌号码"
          type="text"
          placeholder="请输入车牌的号码"
          v-model="form.carnum"
          v-validate="'required'"
          data-vv-validate-on="input"
          :error="errors.first('required')"
        ></md-input-item>

        <md-input-item
          title="租金"
          type="money"
          placeholder="请输入租金"
          v-model="form.car_fee"
        ></md-input-item>
        <md-input-item
          title="保证金"
          type="money"
          placeholder="保证金"
          v-model="form.despoil_fee"
        ></md-input-item>
        <md-field-item
          name="name"
          title="自驾/代驾"
          align="left"
          :value="orderinfo.need_driver==1? '代驾':'自驾'">
        </md-field-item>
        <md-input-item
          title="司机费用"
          type="money"
          placeholder="请输入司机费用"
          v-model="form.driver_fee"
        ></md-input-item>
        <md-input-item
          title="取车里程(km)"
          type="money"
          placeholder="取车里程"
          v-model="form.sdistance"
        ></md-input-item>
        <md-input-item
          title="取车油量(%)"
          placeholder="取车油量"
          v-model="form.yl"
        ></md-input-item>

        <md-input-item
          title="服务费"
          type="money"
          placeholder="服务费"
          v-model="form.service_fee"
        ></md-input-item>
        <md-input-item
          title="手续费"
          type="money"
          placeholder="手续费"
          v-model="form.ss_fee"
        ></md-input-item>
        <md-input-item
          title="上门服务费"
          type="money"
          placeholder="上门服务费"
          v-model="form.send_fee"
        ></md-input-item>


      </md-field>

      <md-button @click="doCheck" v-if="orderinfo.state==1" >确认订单</md-button>
      <md-button disabled>
        <span v-if="orderinfo.state===-1">已取消</span>
        <span v-if="orderinfo.state===0">未支付</span>
        <span v-if="orderinfo.state===2">等待还车</span>
        <span v-if="orderinfo.state===3">违章确认期</span>
        <span v-if="orderinfo.state===4">有违章需要处理</span>
        <span v-if="orderinfo.state===5">已完成</span>
      </md-button>

    </div>
</template>

<script>
import { Toast } from 'mand-mobile'
export default {
  name: 'order',
  data () {
    return {
      currentDate: new Date(),
      showSdate: false,
      form: {
        need_driver: false,
        type: '',
        cid: '',
        sdate: new Date(),
        edate: new Date(),
        carnum: '',
        despoil_fee: 0.00,
        fee: 0.00,
        sdistance: 0,
        yl: 100,
        ss_fee: 20,
        service_fee: 100,
        send_fee: 0.00,
        car_fee: 0.00,
        insurance_fee: 0.00

      },
      returninfo: {
        edistance: 0,
        return_time: new Date(),
        needwash: true,
        over_distant_fee: 0.00,
        over_time_fee: 0.00,
        other_fee: 0.00

      },
      carlist: [],
      orderinfo: {},
      step: 0,
      sfzlist: [],
      jszlist: [],
      signoption: {
        penColor: 'rgb(0, 0, 0)',
        backgroundColor: 'rgb(240,240,240)'
      },
      feestander: {
        dayfee: {},
        fourfee: {},
        eightfee: {}
      }

    }
  },
  created: async function () {
    Toast.loading('加载中')
    await this.getcarlist()
    await this.getorder()

    Toast.hide()
  },
  beforeMount: async function () {

  },
  computed: {
    daycount: function () {
      let _d = parseInt((this.form.edate - this.form.sdate) / 1000)
      let _ct = Math.ceil(_d / (24 * 60 * 60))
      return _ct
    },
    total_fee: function () {
      let _df = this.form.need_driver ? this.form.driver_fee : 0.00
      let _fee = parseFloat(this.form.send_fee) +
          parseFloat(this.form.car_fee) + parseFloat(this.form.service_fee) +
          parseFloat(this.form.despoil_fee) + parseFloat(this.form.insurance_fee) + parseFloat(_df)

      return parseFloat(_fee)
    }

  },
  watch: {
    'form.type': function () {
      console.log(this.feestander)
      if (this.form.type === '4小时') {
        let _d = new Date(this.form.sdate)
        _d.setHours(_d.getHours() + 4)
        this.form.edate = _d
        this.form.car_fee = this.feestander.fourfee.fee
      } else if (this.form.type === '8小时') {
        let _d = new Date(this.form.sdate)
        _d.setHours(_d.getHours() + 8)
        this.form.edate = _d
        this.form.car_fee = this.feestander.eightfee.fee
      } else {
        try {
          this.form.edate = this.form.sdate.getTime() + parseInt(this.orderinfo.e_date) - parseInt(this.orderinfo.s_date)
        } catch (e) {
          this.form.edate = this.form.sdate + parseInt(this.orderinfo.e_date) - parseInt(this.orderinfo.s_date)
        }

        let _d = parseInt((this.form.edate - this.form.sdate) / 1000)
        let _ct = Math.ceil(_d / (24 * 60 * 60))
        this.form.car_fee = parseFloat(_ct * this.feestander.dayfee.fee)
      }
    },
    'step': function () {
      console.log(this.step)
      if (this.step === 2) {
        /* if(this.orderinfo.state===3){
             this.step = 3
           }

           if(this.orderinfo.state===4){
             this.step = 4
           } */
      }
    }
  },
  mounted: async function () {

  },
  methods: {
    getcarlist: async function () {
      let r = await this.$api.post('/admin/carlist')
      if (r.data) {
        this.carlist = r.data.rows
      }
    },
    countfee: async function () {
      if (this.form.type === '4小时') {
        let _d = new Date(this.form.sdate)
        _d.setHours(_d.getHours() + 4)
        this.form.edate = _d
        this.form.car_fee = this.feestander.fourfee.fee
        this.form.driver_fee = this.feestander.fourfee.driver_fee
        this.form.insurance_fee = 100.00
      } else if (this.form.type === '8小时') {
        let _d = new Date(this.form.sdate)
        _d.setHours(_d.getHours() + 8)
        this.form.edate = _d
        this.form.car_fee = this.feestander.eightfee.fee
        this.form.driver_fee = this.feestander.eightfee.driver_fee
        this.form.insurance_fee = 100.00
      } else {
        if (this.form.edate < this.form.sdate) {
          this.form.edate = this.form.sdate.getTime() + 36 * 60 * 60 * 1000
        }
        this.form.car_fee = parseFloat(this.daycount * this.feestander.dayfee.fee)
        this.form.driver_fee = parseFloat(this.daycount * this.feestander.dayfee.driver_fee)
        this.form.insurance_fee = parseFloat(this.daycount * 100.00)
      }
    },
    getorder: async function () {
      let r = await this.$api.post('/admin/orderinfo', {oid: this.$route.query.oid})
      if (r.data.state) {
        this.orderinfo = r.data.data
        this.form.cid = this.orderinfo.cid
        this.form.sdate = new Date().setTime(this.orderinfo.s_date)
        this.form.edate = new Date().setTime(this.orderinfo.e_date)
        this.form.need_driver = !!this.orderinfo.need_driver
        if (this.orderinfo.type === 1) {
          this.form.type = '4小时'
        } else if (this.orderinfo.type === 2) {
          this.form.type = '8小时'
        } else {
          this.form.type = '1天'
        }
        this.form.insurance_fee = this.orderinfo.insurance_fee
        this.form.driver_fee = this.orderinfo.driver_fee
        this.form.fee = this.orderinfo.fee
        this.form.despoil_fee = this.orderinfo.despoil_fee
        this.form.address = this.orderinfo.address
        this.form.service_fee = this.orderinfo.service_fee
        this.form.send_fee = this.orderinfo.send_fee
        this.form.car_fee = this.orderinfo.car_fee
        this.form.sdistance = this.orderinfo.sdistance ? this.orderinfo.sdistance : 0
        this.form.carnum = this.orderinfo.carnum === "null"  ?  '' : this.orderinfo.carnum

        if (this.orderinfo.state === 2) {
          this.step = 3
        }

        if (this.orderinfo.state === 3) {
          this.step = 4
        }
        const {id_photo_a, id_photo_b, license_a, license_b } = this.orderinfo.user
        if (id_photo_a) {
          this.sfzlist.push({name: '身份证正面', url: id_photo_a})
        }
        if (id_photo_b) {
          this.sfzlist.push({name: '身份证反面', url: id_photo_b})
        }
        if (license_a) {
          this.jszlist.push({name: '驾驶证正面', url: license_a})
        }
        if (license_b) {
          this.jszlist.push({name: '驾驶证反面', url: license_b})
        }

        // 在carlist 中获取 收费标准
        let _temfl = []

        for (let v of this.carlist) {
          for (let k of v.feestanders) {
            _temfl.push(k)
          }
        }

        for (let v of _temfl) {
          if (v.cid === this.orderinfo.cid) {
            if (v.hours === 24) {
              this.feestander.dayfee = v
            } else if (v.hours === 8) {
              this.feestander.eightfee = v
            } else if (v.hours === 4) {
              this.feestander.fourfee = v
            }
          }
        }
      } else {
        this.$message.error('获取信息错误')
        setTimeout(() => {
          this.$router.go(-1)
        }, 2000)
      }
    },
    doCheck: async function () {
      let updateinfo = JSON.parse(JSON.stringify(this.form)) // 深拷贝

      //  订单id
      updateinfo.oid = this.orderinfo.oid

      // 订单状态修改为2 取车
      updateinfo.state = 2

      // 剔除 ss
      delete updateinfo.ss_fee
      delete updateinfo.sdate
      delete updateinfo.edate

      let _r = await this.$api.post('/updateorder', updateinfo)
      if (_r.data.state) {
      Toast.succeed('确认成功')
        setTimeout(() => {
          this.$router.go(-1)
        }, 1500)
      } else {
        Toast.failed('确认失败')
      }
    }
  }
}
</script>

<style scoped>
h1{
  height: 45px;
  background: #e9e9e9;
  text-align: center;
  font-size: 14px;
  line-height: 45px;
}
</style>
